﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using cocos2d;

namespace CampaignWP7.Base
{
    public class DynamicsObj : CCSprite
    {
        public static Vector2 sGravity = new Vector2(0,0f);
        public static Vector2 Gravity
        {
            get { return sGravity; }
        }
        //////////////////////////////////////////////////////////////////////////
        protected Vector2 m_velocity;
        protected Vector2 m_acceleration;
        protected CCPoint m_spinPos;
        protected bool m_bSpin;
        public bool RotationEnable { set; get; }
        public bool EffectiveByGravity { set; get; }
        //
        public DynamicsObj()
        {
            this.anchorPoint = new CCPoint(0.5f, 0.5f);
            m_acceleration = new Vector2(0, 0);
            RotationEnable = false;
            EffectiveByGravity = true;
        }
        //
        public override void update(float dt)
        {
            base.update(dt);
            if (m_bSpin)
            {
                position = m_spinPos;
                return;
            }
            //
            m_velocity = m_velocity + m_acceleration * dt;
            if (EffectiveByGravity)
            {
                m_velocity = m_velocity + DynamicsObj.Gravity * dt;                
            }
            //
            Vector2 oldPos = new Vector2(this.position.x, this.position.y);
            Vector2 newPos = oldPos + m_velocity;
            this.position.x = newPos.X;
            this.position.y = newPos.Y;
            if (RotationEnable)
            {
                Vector2 direction = new Vector2(m_velocity.X, m_velocity.Y);
                direction.Normalize();
                float cosed_angle = Vector2.Dot(direction, new Vector2(0, 1));
                //
                
                float angle = (float)Math.Acos(cosed_angle);
                if (cosed_angle < 0) angle = (float)Math.PI/2 - angle;
                base.rotation = 1-(angle / (float)Math.PI) * 180;
            }
            else
            {
                base.rotation = 0;
            }

        }
        public Vector2 Velocity
        {
            get { return m_velocity; }
            set { m_velocity = value; }
        }
        public Vector2 Acceleration
        {
            get { return m_acceleration; }
            set { m_acceleration = value; }
        }
        public float Speed()
        {
            return m_velocity.Length();
        }
        //
        public void Spin(bool spin,CCPoint pos)
        {
            m_bSpin = spin;
            if (m_bSpin)
            {
                if (pos != null)
                {
                    m_spinPos = pos;
                }
                else
                {
                    m_spinPos = position;
                }
            }

        }
        
    }
}
